/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#pragma once

#include "../结构数据.h"
#include <basic_vec.h>


Inline bool f_intersect_点在矩形内(const S_Bounding2& bound, vec2 p) {
	return (p.x >= bound.min.x - 1e-6 && p.x <= bound.max.x + 1e-6 &&
			p.y >= bound.min.y - 1e-6 && p.y <= bound.max.y + 1e-6);
}

Inline bool f_intersect_点在三角形内(const vec2& p, const vec2 traingle[3]) {
	
	// 计算三个叉积，判断点与三条边的位置关系
	float32 cross1 = cross(traingle[0], traingle[1], p);
	float32 cross2 = cross(traingle[1], traingle[2], p);
	float32 cross3 = cross(traingle[2], traingle[0], p);

	
	const float32 eps = 1e-6f;
	// 检查三个叉积是否同号（都非正或都非负）
	bool has_neg = (cross1 < 0) || (cross2 < 0) || (cross3 < 0);
	bool has_pos = (cross1 > 0) || (cross2 > 0) || (cross3 > 0);

	// 如果不同时存在正负，则点在三角形内或边上
	return !(has_neg && has_pos);
}

//Inline bool f_intersect_点在多边形内(vec2 p) {
//	bool inside = false;
//	uint32 num = vert_proj.size();
//	for (int32 i = 0, k = num - 1; i < num; k=i, ++i) {
//		//uvec2 index_r = index[i];
//		vec2 开始顶点坐标 = vert_proj[k];
//		vec2 结束顶点坐标 = vert_proj[i];
//
//		// 检查点是否在边上
//		vec2 edge = 开始顶点坐标 - 结束顶点坐标;
//		vec2 toPoint = p - 结束顶点坐标;
//		float32 crs = cross(edge, toPoint);
//
//		if (fabs(crs) < 1e-8) {
//			float32 minX = min(结束顶点坐标.x, 开始顶点坐标.x);
//			float32 maxX = max(结束顶点坐标.x, 开始顶点坐标.x);
//			float32 minY = min(结束顶点坐标.y, 开始顶点坐标.y);
//			float32 maxY = max(结束顶点坐标.y, 开始顶点坐标.y);
//
//			if (p.x >= minX - 1e-8 && p.x <= maxX + 1e-8 &&
//				p.y >= minY - 1e-8 && p.y <= maxY + 1e-8) {
//				return true;
//			}
//		}
//
//
//		if ( ((结束顶点坐标.y > p.y) != (开始顶点坐标.y > p.y)) &&
//			(p.x < 结束顶点坐标.x + (开始顶点坐标.x - 结束顶点坐标.x) * (p.y - 结束顶点坐标.y) / (开始顶点坐标.y - 结束顶点坐标.y))
//			) {
//			inside = !inside;
//		}
//	}
//	return inside;
//};


bool f_intersect_线段相交(vec2 a1, vec2 a2, vec2 b1, vec2 b2);



//Inline bool f_intersect_点在三角形内(const vec2& p, const vec2 traingle[3]) {
//
//	//float32 cross1 = cross(traingle[0], traingle[1], p);
//	//float32 cross2 = cross(traingle[1], traingle[2], p);
//	//float32 cross3 = cross(traingle[2], traingle[0], p);
//
//	float32 cross1 = cross(traingle[0] - traingle[1], traingle[1] - p);
//	float32 cross2 = cross(traingle[1] - traingle[2], traingle[2] - p);
//	float32 cross3 = cross(traingle[2] - traingle[0], traingle[0] - p);
//
//	//bool has_neg = (cross1 < 0) || (cross2 < 0) || (cross3 < 0);
//	//bool has_pos = (cross1 > 0) || (cross2 > 0) || (cross3 > 0);
//
//	auto a = sign(cross1) + sign(cross2) + sign(cross3);
//	return fabs(a) == 3;
//}



bool f_intersect_RayAndAABB(float32 rayOrigin[3], float32 rayDirection[3], float32 boxMin[3], float32 boxMax[3]);

int32	f_intersect_射线三角形(const vec3& vert, const vec3& 边A, const vec3& 边B, const vec3& S, const vec3& dir, vec3* uvt);
bool	f_intersect_射线三角形(const vec3& v1, const vec3& v2, const vec3& v3, const vec3& S, const vec3& dir);

bool	f_intersect_点在多边形内(vec2 point, const vec2* polygon, uint32 num);







